/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.audio.yzserver.utils;

import cn.ac.iie.di.ban.ycl.wx.audio.yzserver.common.RuntimeEnv;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class Suffixutil {

    static Logger logger = Logger.getLogger(Suffixutil.class.getName());

    /**
     *
     * @param url,通过url判断文件的后缀名是什么，
     * @return
     */
    public static String suffix(String url) {
//        String suffixestr = ".m4a|.avi|.mpeg|.3gp|.mp3|.mp4|.wav|.jpeg|.gif|.jpg|.png|.apk|.exe|.pdf|.rar|.zip|.docx|.doc";
        String suffix = null;
        try {

            String suffixestr = RuntimeEnv.getParam(RuntimeEnv.suffixestr) + "";
            Pattern pat = Pattern.compile(suffixestr);
            Matcher d = pat.matcher(url);
            //注意d.find()只能用一次，否则将查找不到
//            System.out.println("find "+d.find()+","+d.group());
            if (d.find()) {
                suffix = d.group();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return suffix;
    }

    public static boolean mkdirs(String fileDir) {
        File f = new File(fileDir);
        if (!f.isDirectory() || !f.exists()) {
            return f.mkdirs();
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        String s = "q.mp3";
//        String s = "http://ws.stream.qqmusic.qq.com/C100003hTE6N2jHGN5.mp4?fromtag=46";

        System.out.println(suffix(s));
    }
}
